//
//  AlbumViewController.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "AlbumViewController.h"

@interface AlbumViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)AlbumModel *albumModel;
@property (nonatomic, strong)UITableView *tableView;


@end

@implementation AlbumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getNetData];
    [self createTableView];
    
    //更改返回按钮样式
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(backAction:)];
    //更改按钮颜色
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor redColor];
    
    self.navigationItem.title = self.hotRecommendListModel.title;
    
}

//播放页面跳转方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PlayViewController *playVC = [PlayViewController sharePlayViewController];
    playVC.sequence = indexPath.row;
    playVC.dataSource = [NSArray arrayWithArray:self.albumModel.AlbumListArray];

    [self presentViewController:playVC animated:YES completion:^{
        
        
    }];
    
    
    
}







//返回按钮的点击方法

- (void)backAction:(UIBarButtonItem *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)getNetData {

    NSString *string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/20?albumId=%@&isAsc=true&device=android&pageSize=20", self.hotRecommendListModel.albumId, self.hotRecommendListModel.albumId];
    [WXCNetWorkTool getWithUrl:string parameter:nil httpHeader:nil responseType:ResponseTypeJson success:^(id result) {

        //解model
        self.albumModel = [[AlbumModel alloc] initWithDictionary:result[@"album"]];
        self.albumModel.AlbumListArray = [NSMutableArray array];
        NSArray *array = result[@"tracks"][@"list"];
        for (NSDictionary *dic in array) {
            AlbumListModel *listModle = [[AlbumListModel alloc] initWithDictionary:dic];
            [self.albumModel.AlbumListArray addObject:listModle];
        }
        
        [self.tableView reloadData];
    } fail:^(NSError *error) {
        
        
    }];
}

- (void)createTableView {
    //加入tabbar后不用减去bar的高度49
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[AlbumTableViewCell class] forCellReuseIdentifier:@"AlbumTableViewCellIdentifier"];
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.albumModel.AlbumListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AlbumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AlbumTableViewCellIdentifier"];
    cell.albumListModel = self.albumModel.AlbumListArray[indexPath.row];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    
    
    if (self.albumModel.AlbumListArray.count) {
        AlbumHeaderView *headerView = [[AlbumHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 150)];
        headerView.backgroundColor = [UIColor whiteColor];
        //相关推荐跳转
        headerView.block = ^(NSString *string) {
            RelateViewController *relateVC = [[RelateViewController alloc] init];
            relateVC.albumID = string;
            [self.navigationController pushViewController:relateVC animated:YES];
        };
        
       
        
        
        headerView.albumModel = self.albumModel;
        return headerView;
    } else {
        return nil;
    }
    

    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 150;
}

//设置行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
//
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}






















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}












/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
